{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 首先 import 必要的模块\n",
    "import pandas as pd \n",
    "import numpy as np\n",
    "\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "\n",
    "#竞赛的评价指标为logloss\n",
    "from sklearn.metrics import log_loss  \n",
    "\n",
    "from matplotlib import pyplot\n",
    "import seaborn as sns\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Pregnancies</th>\n",
       "      <th>Glucose</th>\n",
       "      <th>BloodPressure</th>\n",
       "      <th>SkinThickness</th>\n",
       "      <th>Insulin</th>\n",
       "      <th>BMI</th>\n",
       "      <th>DiabetesPedigreeFunction</th>\n",
       "      <th>Age</th>\n",
       "      <th>Outcome</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>6</td>\n",
       "      <td>148</td>\n",
       "      <td>72</td>\n",
       "      <td>35</td>\n",
       "      <td>0</td>\n",
       "      <td>33.6</td>\n",
       "      <td>0.627</td>\n",
       "      <td>50</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>29</td>\n",
       "      <td>0</td>\n",
       "      <td>26.6</td>\n",
       "      <td>0.351</td>\n",
       "      <td>31</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>8</td>\n",
       "      <td>183</td>\n",
       "      <td>64</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>23.3</td>\n",
       "      <td>0.672</td>\n",
       "      <td>32</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>89</td>\n",
       "      <td>66</td>\n",
       "      <td>23</td>\n",
       "      <td>94</td>\n",
       "      <td>28.1</td>\n",
       "      <td>0.167</td>\n",
       "      <td>21</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>137</td>\n",
       "      <td>40</td>\n",
       "      <td>35</td>\n",
       "      <td>168</td>\n",
       "      <td>43.1</td>\n",
       "      <td>2.288</td>\n",
       "      <td>33</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Pregnancies  Glucose  BloodPressure  SkinThickness  Insulin   BMI  \\\n",
       "0            6      148             72             35        0  33.6   \n",
       "1            1       85             66             29        0  26.6   \n",
       "2            8      183             64              0        0  23.3   \n",
       "3            1       89             66             23       94  28.1   \n",
       "4            0      137             40             35      168  43.1   \n",
       "\n",
       "   DiabetesPedigreeFunction  Age  Outcome  \n",
       "0                     0.627   50        1  \n",
       "1                     0.351   31        0  \n",
       "2                     0.672   32        1  \n",
       "3                     0.167   21        0  \n",
       "4                     2.288   33        1  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train = pd.read_csv('D:\\CSDN\\week2-svm\\diabetes.csv')\n",
    "train.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据探索/特征工程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 768 entries, 0 to 767\n",
      "Data columns (total 9 columns):\n",
      "Pregnancies                 768 non-null int64\n",
      "Glucose                     768 non-null int64\n",
      "BloodPressure               768 non-null int64\n",
      "SkinThickness               768 non-null int64\n",
      "Insulin                     768 non-null int64\n",
      "BMI                         768 non-null float64\n",
      "DiabetesPedigreeFunction    768 non-null float64\n",
      "Age                         768 non-null int64\n",
      "Outcome                     768 non-null int64\n",
      "dtypes: float64(2), int64(7)\n",
      "memory usage: 54.1 KB\n"
     ]
    }
   ],
   "source": [
    "train.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Pregnancies                 374\n",
       "Glucose                     374\n",
       "BloodPressure               374\n",
       "SkinThickness               374\n",
       "Insulin                     374\n",
       "BMI                         374\n",
       "DiabetesPedigreeFunction    374\n",
       "Age                         374\n",
       "Outcome                     374\n",
       "dtype: int64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train[train['Insulin']==0].count()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Insulin这个特征对于预测结果是如此重要，但又有接近一半是空值，所以不能去掉这个特征，宁缺勿滥，把Insulin=0的样本都去掉"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Pregnancies</th>\n",
       "      <th>Glucose</th>\n",
       "      <th>BloodPressure</th>\n",
       "      <th>SkinThickness</th>\n",
       "      <th>Insulin</th>\n",
       "      <th>BMI</th>\n",
       "      <th>DiabetesPedigreeFunction</th>\n",
       "      <th>Age</th>\n",
       "      <th>Outcome</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>6</td>\n",
       "      <td>148</td>\n",
       "      <td>72</td>\n",
       "      <td>35</td>\n",
       "      <td>0</td>\n",
       "      <td>33.6</td>\n",
       "      <td>0.627</td>\n",
       "      <td>50</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>29</td>\n",
       "      <td>0</td>\n",
       "      <td>26.6</td>\n",
       "      <td>0.351</td>\n",
       "      <td>31</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>8</td>\n",
       "      <td>183</td>\n",
       "      <td>64</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>23.3</td>\n",
       "      <td>0.672</td>\n",
       "      <td>32</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5</td>\n",
       "      <td>116</td>\n",
       "      <td>74</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>25.6</td>\n",
       "      <td>0.201</td>\n",
       "      <td>30</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>10</td>\n",
       "      <td>115</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>35.3</td>\n",
       "      <td>0.134</td>\n",
       "      <td>29</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>8</td>\n",
       "      <td>125</td>\n",
       "      <td>96</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.232</td>\n",
       "      <td>54</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>4</td>\n",
       "      <td>110</td>\n",
       "      <td>92</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>37.6</td>\n",
       "      <td>0.191</td>\n",
       "      <td>30</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>10</td>\n",
       "      <td>168</td>\n",
       "      <td>74</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>38.0</td>\n",
       "      <td>0.537</td>\n",
       "      <td>34</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>10</td>\n",
       "      <td>139</td>\n",
       "      <td>80</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>27.1</td>\n",
       "      <td>1.441</td>\n",
       "      <td>57</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>7</td>\n",
       "      <td>100</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>0.484</td>\n",
       "      <td>32</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>7</td>\n",
       "      <td>107</td>\n",
       "      <td>74</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>29.6</td>\n",
       "      <td>0.254</td>\n",
       "      <td>31</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>8</td>\n",
       "      <td>99</td>\n",
       "      <td>84</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>35.4</td>\n",
       "      <td>0.388</td>\n",
       "      <td>50</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>7</td>\n",
       "      <td>196</td>\n",
       "      <td>90</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>39.8</td>\n",
       "      <td>0.451</td>\n",
       "      <td>41</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>9</td>\n",
       "      <td>119</td>\n",
       "      <td>80</td>\n",
       "      <td>35</td>\n",
       "      <td>0</td>\n",
       "      <td>29.0</td>\n",
       "      <td>0.263</td>\n",
       "      <td>29</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>7</td>\n",
       "      <td>147</td>\n",
       "      <td>76</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>39.4</td>\n",
       "      <td>0.257</td>\n",
       "      <td>43</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>5</td>\n",
       "      <td>117</td>\n",
       "      <td>92</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>34.1</td>\n",
       "      <td>0.337</td>\n",
       "      <td>38</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>5</td>\n",
       "      <td>109</td>\n",
       "      <td>75</td>\n",
       "      <td>26</td>\n",
       "      <td>0</td>\n",
       "      <td>36.0</td>\n",
       "      <td>0.546</td>\n",
       "      <td>60</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>6</td>\n",
       "      <td>92</td>\n",
       "      <td>92</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>19.9</td>\n",
       "      <td>0.188</td>\n",
       "      <td>28</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>10</td>\n",
       "      <td>122</td>\n",
       "      <td>78</td>\n",
       "      <td>31</td>\n",
       "      <td>0</td>\n",
       "      <td>27.6</td>\n",
       "      <td>0.512</td>\n",
       "      <td>45</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>11</td>\n",
       "      <td>138</td>\n",
       "      <td>76</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>33.2</td>\n",
       "      <td>0.420</td>\n",
       "      <td>35</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>9</td>\n",
       "      <td>102</td>\n",
       "      <td>76</td>\n",
       "      <td>37</td>\n",
       "      <td>0</td>\n",
       "      <td>32.9</td>\n",
       "      <td>0.665</td>\n",
       "      <td>46</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>2</td>\n",
       "      <td>90</td>\n",
       "      <td>68</td>\n",
       "      <td>42</td>\n",
       "      <td>0</td>\n",
       "      <td>38.2</td>\n",
       "      <td>0.503</td>\n",
       "      <td>27</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>7</td>\n",
       "      <td>133</td>\n",
       "      <td>84</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>40.2</td>\n",
       "      <td>0.696</td>\n",
       "      <td>37</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>7</td>\n",
       "      <td>106</td>\n",
       "      <td>92</td>\n",
       "      <td>18</td>\n",
       "      <td>0</td>\n",
       "      <td>22.7</td>\n",
       "      <td>0.235</td>\n",
       "      <td>48</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>44</th>\n",
       "      <td>7</td>\n",
       "      <td>159</td>\n",
       "      <td>64</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>27.4</td>\n",
       "      <td>0.294</td>\n",
       "      <td>40</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>45</th>\n",
       "      <td>0</td>\n",
       "      <td>180</td>\n",
       "      <td>66</td>\n",
       "      <td>39</td>\n",
       "      <td>0</td>\n",
       "      <td>42.0</td>\n",
       "      <td>1.893</td>\n",
       "      <td>25</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>46</th>\n",
       "      <td>1</td>\n",
       "      <td>146</td>\n",
       "      <td>56</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>29.7</td>\n",
       "      <td>0.564</td>\n",
       "      <td>29</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>47</th>\n",
       "      <td>2</td>\n",
       "      <td>71</td>\n",
       "      <td>70</td>\n",
       "      <td>27</td>\n",
       "      <td>0</td>\n",
       "      <td>28.0</td>\n",
       "      <td>0.586</td>\n",
       "      <td>22</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>48</th>\n",
       "      <td>7</td>\n",
       "      <td>103</td>\n",
       "      <td>66</td>\n",
       "      <td>32</td>\n",
       "      <td>0</td>\n",
       "      <td>39.1</td>\n",
       "      <td>0.344</td>\n",
       "      <td>31</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49</th>\n",
       "      <td>7</td>\n",
       "      <td>105</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.305</td>\n",
       "      <td>24</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>712</th>\n",
       "      <td>10</td>\n",
       "      <td>129</td>\n",
       "      <td>62</td>\n",
       "      <td>36</td>\n",
       "      <td>0</td>\n",
       "      <td>41.2</td>\n",
       "      <td>0.441</td>\n",
       "      <td>38</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>714</th>\n",
       "      <td>3</td>\n",
       "      <td>102</td>\n",
       "      <td>74</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>29.5</td>\n",
       "      <td>0.121</td>\n",
       "      <td>32</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>717</th>\n",
       "      <td>10</td>\n",
       "      <td>94</td>\n",
       "      <td>72</td>\n",
       "      <td>18</td>\n",
       "      <td>0</td>\n",
       "      <td>23.1</td>\n",
       "      <td>0.595</td>\n",
       "      <td>56</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>719</th>\n",
       "      <td>5</td>\n",
       "      <td>97</td>\n",
       "      <td>76</td>\n",
       "      <td>27</td>\n",
       "      <td>0</td>\n",
       "      <td>35.6</td>\n",
       "      <td>0.378</td>\n",
       "      <td>52</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>720</th>\n",
       "      <td>4</td>\n",
       "      <td>83</td>\n",
       "      <td>86</td>\n",
       "      <td>19</td>\n",
       "      <td>0</td>\n",
       "      <td>29.3</td>\n",
       "      <td>0.317</td>\n",
       "      <td>34</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>724</th>\n",
       "      <td>1</td>\n",
       "      <td>111</td>\n",
       "      <td>94</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>32.8</td>\n",
       "      <td>0.265</td>\n",
       "      <td>45</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>725</th>\n",
       "      <td>4</td>\n",
       "      <td>112</td>\n",
       "      <td>78</td>\n",
       "      <td>40</td>\n",
       "      <td>0</td>\n",
       "      <td>39.4</td>\n",
       "      <td>0.236</td>\n",
       "      <td>38</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>727</th>\n",
       "      <td>0</td>\n",
       "      <td>141</td>\n",
       "      <td>84</td>\n",
       "      <td>26</td>\n",
       "      <td>0</td>\n",
       "      <td>32.4</td>\n",
       "      <td>0.433</td>\n",
       "      <td>22</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>728</th>\n",
       "      <td>2</td>\n",
       "      <td>175</td>\n",
       "      <td>88</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>22.9</td>\n",
       "      <td>0.326</td>\n",
       "      <td>22</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>729</th>\n",
       "      <td>2</td>\n",
       "      <td>92</td>\n",
       "      <td>52</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>30.1</td>\n",
       "      <td>0.141</td>\n",
       "      <td>22</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>731</th>\n",
       "      <td>8</td>\n",
       "      <td>120</td>\n",
       "      <td>86</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>28.4</td>\n",
       "      <td>0.259</td>\n",
       "      <td>22</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>734</th>\n",
       "      <td>2</td>\n",
       "      <td>105</td>\n",
       "      <td>75</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>23.3</td>\n",
       "      <td>0.560</td>\n",
       "      <td>53</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>735</th>\n",
       "      <td>4</td>\n",
       "      <td>95</td>\n",
       "      <td>60</td>\n",
       "      <td>32</td>\n",
       "      <td>0</td>\n",
       "      <td>35.4</td>\n",
       "      <td>0.284</td>\n",
       "      <td>28</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>737</th>\n",
       "      <td>8</td>\n",
       "      <td>65</td>\n",
       "      <td>72</td>\n",
       "      <td>23</td>\n",
       "      <td>0</td>\n",
       "      <td>32.0</td>\n",
       "      <td>0.600</td>\n",
       "      <td>42</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>739</th>\n",
       "      <td>1</td>\n",
       "      <td>102</td>\n",
       "      <td>74</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>39.5</td>\n",
       "      <td>0.293</td>\n",
       "      <td>42</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>743</th>\n",
       "      <td>9</td>\n",
       "      <td>140</td>\n",
       "      <td>94</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>32.7</td>\n",
       "      <td>0.734</td>\n",
       "      <td>45</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>746</th>\n",
       "      <td>1</td>\n",
       "      <td>147</td>\n",
       "      <td>94</td>\n",
       "      <td>41</td>\n",
       "      <td>0</td>\n",
       "      <td>49.3</td>\n",
       "      <td>0.358</td>\n",
       "      <td>27</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>749</th>\n",
       "      <td>6</td>\n",
       "      <td>162</td>\n",
       "      <td>62</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>24.3</td>\n",
       "      <td>0.178</td>\n",
       "      <td>50</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>750</th>\n",
       "      <td>4</td>\n",
       "      <td>136</td>\n",
       "      <td>70</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>31.2</td>\n",
       "      <td>1.182</td>\n",
       "      <td>22</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>752</th>\n",
       "      <td>3</td>\n",
       "      <td>108</td>\n",
       "      <td>62</td>\n",
       "      <td>24</td>\n",
       "      <td>0</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0.223</td>\n",
       "      <td>25</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>754</th>\n",
       "      <td>8</td>\n",
       "      <td>154</td>\n",
       "      <td>78</td>\n",
       "      <td>32</td>\n",
       "      <td>0</td>\n",
       "      <td>32.4</td>\n",
       "      <td>0.443</td>\n",
       "      <td>45</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>756</th>\n",
       "      <td>7</td>\n",
       "      <td>137</td>\n",
       "      <td>90</td>\n",
       "      <td>41</td>\n",
       "      <td>0</td>\n",
       "      <td>32.0</td>\n",
       "      <td>0.391</td>\n",
       "      <td>39</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>757</th>\n",
       "      <td>0</td>\n",
       "      <td>123</td>\n",
       "      <td>72</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>36.3</td>\n",
       "      <td>0.258</td>\n",
       "      <td>52</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>758</th>\n",
       "      <td>1</td>\n",
       "      <td>106</td>\n",
       "      <td>76</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>37.5</td>\n",
       "      <td>0.197</td>\n",
       "      <td>26</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>759</th>\n",
       "      <td>6</td>\n",
       "      <td>190</td>\n",
       "      <td>92</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>35.5</td>\n",
       "      <td>0.278</td>\n",
       "      <td>66</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>761</th>\n",
       "      <td>9</td>\n",
       "      <td>170</td>\n",
       "      <td>74</td>\n",
       "      <td>31</td>\n",
       "      <td>0</td>\n",
       "      <td>44.0</td>\n",
       "      <td>0.403</td>\n",
       "      <td>43</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>762</th>\n",
       "      <td>9</td>\n",
       "      <td>89</td>\n",
       "      <td>62</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>22.5</td>\n",
       "      <td>0.142</td>\n",
       "      <td>33</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>764</th>\n",
       "      <td>2</td>\n",
       "      <td>122</td>\n",
       "      <td>70</td>\n",
       "      <td>27</td>\n",
       "      <td>0</td>\n",
       "      <td>36.8</td>\n",
       "      <td>0.340</td>\n",
       "      <td>27</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>766</th>\n",
       "      <td>1</td>\n",
       "      <td>126</td>\n",
       "      <td>60</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>30.1</td>\n",
       "      <td>0.349</td>\n",
       "      <td>47</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>767</th>\n",
       "      <td>1</td>\n",
       "      <td>93</td>\n",
       "      <td>70</td>\n",
       "      <td>31</td>\n",
       "      <td>0</td>\n",
       "      <td>30.4</td>\n",
       "      <td>0.315</td>\n",
       "      <td>23</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>376 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     Pregnancies  Glucose  BloodPressure  SkinThickness  Insulin   BMI  \\\n",
       "0              6      148             72             35        0  33.6   \n",
       "1              1       85             66             29        0  26.6   \n",
       "2              8      183             64              0        0  23.3   \n",
       "5              5      116             74              0        0  25.6   \n",
       "7             10      115              0              0        0  35.3   \n",
       "9              8      125             96              0        0   0.0   \n",
       "10             4      110             92              0        0  37.6   \n",
       "11            10      168             74              0        0  38.0   \n",
       "12            10      139             80              0        0  27.1   \n",
       "15             7      100              0              0        0  30.0   \n",
       "17             7      107             74              0        0  29.6   \n",
       "21             8       99             84              0        0  35.4   \n",
       "22             7      196             90              0        0  39.8   \n",
       "23             9      119             80             35        0  29.0   \n",
       "26             7      147             76              0        0  39.4   \n",
       "29             5      117             92              0        0  34.1   \n",
       "30             5      109             75             26        0  36.0   \n",
       "33             6       92             92              0        0  19.9   \n",
       "34            10      122             78             31        0  27.6   \n",
       "36            11      138             76              0        0  33.2   \n",
       "37             9      102             76             37        0  32.9   \n",
       "38             2       90             68             42        0  38.2   \n",
       "41             7      133             84              0        0  40.2   \n",
       "42             7      106             92             18        0  22.7   \n",
       "44             7      159             64              0        0  27.4   \n",
       "45             0      180             66             39        0  42.0   \n",
       "46             1      146             56              0        0  29.7   \n",
       "47             2       71             70             27        0  28.0   \n",
       "48             7      103             66             32        0  39.1   \n",
       "49             7      105              0              0        0   0.0   \n",
       "..           ...      ...            ...            ...      ...   ...   \n",
       "712           10      129             62             36        0  41.2   \n",
       "714            3      102             74              0        0  29.5   \n",
       "717           10       94             72             18        0  23.1   \n",
       "719            5       97             76             27        0  35.6   \n",
       "720            4       83             86             19        0  29.3   \n",
       "724            1      111             94              0        0  32.8   \n",
       "725            4      112             78             40        0  39.4   \n",
       "727            0      141             84             26        0  32.4   \n",
       "728            2      175             88              0        0  22.9   \n",
       "729            2       92             52              0        0  30.1   \n",
       "731            8      120             86              0        0  28.4   \n",
       "734            2      105             75              0        0  23.3   \n",
       "735            4       95             60             32        0  35.4   \n",
       "737            8       65             72             23        0  32.0   \n",
       "739            1      102             74              0        0  39.5   \n",
       "743            9      140             94              0        0  32.7   \n",
       "746            1      147             94             41        0  49.3   \n",
       "749            6      162             62              0        0  24.3   \n",
       "750            4      136             70              0        0  31.2   \n",
       "752            3      108             62             24        0  26.0   \n",
       "754            8      154             78             32        0  32.4   \n",
       "756            7      137             90             41        0  32.0   \n",
       "757            0      123             72              0        0  36.3   \n",
       "758            1      106             76              0        0  37.5   \n",
       "759            6      190             92              0        0  35.5   \n",
       "761            9      170             74             31        0  44.0   \n",
       "762            9       89             62              0        0  22.5   \n",
       "764            2      122             70             27        0  36.8   \n",
       "766            1      126             60              0        0  30.1   \n",
       "767            1       93             70             31        0  30.4   \n",
       "\n",
       "     DiabetesPedigreeFunction  Age  Outcome  \n",
       "0                       0.627   50        1  \n",
       "1                       0.351   31        0  \n",
       "2                       0.672   32        1  \n",
       "5                       0.201   30        0  \n",
       "7                       0.134   29        0  \n",
       "9                       0.232   54        1  \n",
       "10                      0.191   30        0  \n",
       "11                      0.537   34        1  \n",
       "12                      1.441   57        0  \n",
       "15                      0.484   32        1  \n",
       "17                      0.254   31        1  \n",
       "21                      0.388   50        0  \n",
       "22                      0.451   41        1  \n",
       "23                      0.263   29        1  \n",
       "26                      0.257   43        1  \n",
       "29                      0.337   38        0  \n",
       "30                      0.546   60        0  \n",
       "33                      0.188   28        0  \n",
       "34                      0.512   45        0  \n",
       "36                      0.420   35        0  \n",
       "37                      0.665   46        1  \n",
       "38                      0.503   27        1  \n",
       "41                      0.696   37        0  \n",
       "42                      0.235   48        0  \n",
       "44                      0.294   40        0  \n",
       "45                      1.893   25        1  \n",
       "46                      0.564   29        0  \n",
       "47                      0.586   22        0  \n",
       "48                      0.344   31        1  \n",
       "49                      0.305   24        0  \n",
       "..                        ...  ...      ...  \n",
       "712                     0.441   38        1  \n",
       "714                     0.121   32        0  \n",
       "717                     0.595   56        0  \n",
       "719                     0.378   52        1  \n",
       "720                     0.317   34        0  \n",
       "724                     0.265   45        0  \n",
       "725                     0.236   38        0  \n",
       "727                     0.433   22        0  \n",
       "728                     0.326   22        0  \n",
       "729                     0.141   22        0  \n",
       "731                     0.259   22        1  \n",
       "734                     0.560   53        0  \n",
       "735                     0.284   28        0  \n",
       "737                     0.600   42        0  \n",
       "739                     0.293   42        1  \n",
       "743                     0.734   45        1  \n",
       "746                     0.358   27        1  \n",
       "749                     0.178   50        1  \n",
       "750                     1.182   22        1  \n",
       "752                     0.223   25        0  \n",
       "754                     0.443   45        1  \n",
       "756                     0.391   39        0  \n",
       "757                     0.258   52        1  \n",
       "758                     0.197   26        0  \n",
       "759                     0.278   66        1  \n",
       "761                     0.403   43        1  \n",
       "762                     0.142   33        0  \n",
       "764                     0.340   27        0  \n",
       "766                     0.349   47        1  \n",
       "767                     0.315   23        0  \n",
       "\n",
       "[376 rows x 9 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train[(train['BMI']==0) | (train['Glucose']==0) | (train['BloodPressure']==0) | (train['SkinThickness']==0)| (train['Insulin']==0)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "含有缺失值的样本一共376个，其中Insulin=0占了374个，再多删除2个样本也无所谓i，但可以让样本完全没有缺失值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "train = train[(train['BMI']!=0) & (train['Glucose']!=0) & (train['BloodPressure']!=0) &\n",
    "      (train['SkinThickness']!=0)& (train['Insulin']!=0)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 392 entries, 3 to 765\n",
      "Data columns (total 9 columns):\n",
      "Pregnancies                 392 non-null int64\n",
      "Glucose                     392 non-null int64\n",
      "BloodPressure               392 non-null int64\n",
      "SkinThickness               392 non-null int64\n",
      "Insulin                     392 non-null int64\n",
      "BMI                         392 non-null float64\n",
      "DiabetesPedigreeFunction    392 non-null float64\n",
      "Age                         392 non-null int64\n",
      "Outcome                     392 non-null int64\n",
      "dtypes: float64(2), int64(7)\n",
      "memory usage: 30.6 KB\n"
     ]
    }
   ],
   "source": [
    "train.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Pregnancies</th>\n",
       "      <th>Glucose</th>\n",
       "      <th>BloodPressure</th>\n",
       "      <th>SkinThickness</th>\n",
       "      <th>Insulin</th>\n",
       "      <th>BMI</th>\n",
       "      <th>DiabetesPedigreeFunction</th>\n",
       "      <th>Age</th>\n",
       "      <th>Outcome</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>392.000000</td>\n",
       "      <td>392.000000</td>\n",
       "      <td>392.000000</td>\n",
       "      <td>392.000000</td>\n",
       "      <td>392.000000</td>\n",
       "      <td>392.000000</td>\n",
       "      <td>392.000000</td>\n",
       "      <td>392.000000</td>\n",
       "      <td>392.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>3.301020</td>\n",
       "      <td>122.627551</td>\n",
       "      <td>70.663265</td>\n",
       "      <td>29.145408</td>\n",
       "      <td>156.056122</td>\n",
       "      <td>33.086224</td>\n",
       "      <td>0.523046</td>\n",
       "      <td>30.864796</td>\n",
       "      <td>0.331633</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>3.211424</td>\n",
       "      <td>30.860781</td>\n",
       "      <td>12.496092</td>\n",
       "      <td>10.516424</td>\n",
       "      <td>118.841690</td>\n",
       "      <td>7.027659</td>\n",
       "      <td>0.345488</td>\n",
       "      <td>10.200777</td>\n",
       "      <td>0.471401</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>56.000000</td>\n",
       "      <td>24.000000</td>\n",
       "      <td>7.000000</td>\n",
       "      <td>14.000000</td>\n",
       "      <td>18.200000</td>\n",
       "      <td>0.085000</td>\n",
       "      <td>21.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>99.000000</td>\n",
       "      <td>62.000000</td>\n",
       "      <td>21.000000</td>\n",
       "      <td>76.750000</td>\n",
       "      <td>28.400000</td>\n",
       "      <td>0.269750</td>\n",
       "      <td>23.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>2.000000</td>\n",
       "      <td>119.000000</td>\n",
       "      <td>70.000000</td>\n",
       "      <td>29.000000</td>\n",
       "      <td>125.500000</td>\n",
       "      <td>33.200000</td>\n",
       "      <td>0.449500</td>\n",
       "      <td>27.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>5.000000</td>\n",
       "      <td>143.000000</td>\n",
       "      <td>78.000000</td>\n",
       "      <td>37.000000</td>\n",
       "      <td>190.000000</td>\n",
       "      <td>37.100000</td>\n",
       "      <td>0.687000</td>\n",
       "      <td>36.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>17.000000</td>\n",
       "      <td>198.000000</td>\n",
       "      <td>110.000000</td>\n",
       "      <td>63.000000</td>\n",
       "      <td>846.000000</td>\n",
       "      <td>67.100000</td>\n",
       "      <td>2.420000</td>\n",
       "      <td>81.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       Pregnancies     Glucose  BloodPressure  SkinThickness     Insulin  \\\n",
       "count   392.000000  392.000000     392.000000     392.000000  392.000000   \n",
       "mean      3.301020  122.627551      70.663265      29.145408  156.056122   \n",
       "std       3.211424   30.860781      12.496092      10.516424  118.841690   \n",
       "min       0.000000   56.000000      24.000000       7.000000   14.000000   \n",
       "25%       1.000000   99.000000      62.000000      21.000000   76.750000   \n",
       "50%       2.000000  119.000000      70.000000      29.000000  125.500000   \n",
       "75%       5.000000  143.000000      78.000000      37.000000  190.000000   \n",
       "max      17.000000  198.000000     110.000000      63.000000  846.000000   \n",
       "\n",
       "              BMI  DiabetesPedigreeFunction         Age     Outcome  \n",
       "count  392.000000                392.000000  392.000000  392.000000  \n",
       "mean    33.086224                  0.523046   30.864796    0.331633  \n",
       "std      7.027659                  0.345488   10.200777    0.471401  \n",
       "min     18.200000                  0.085000   21.000000    0.000000  \n",
       "25%     28.400000                  0.269750   23.000000    0.000000  \n",
       "50%     33.200000                  0.449500   27.000000    0.000000  \n",
       "75%     37.100000                  0.687000   36.000000    1.000000  \n",
       "max     67.100000                  2.420000   81.000000    1.000000  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x191a59472b0>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYgAAAEKCAYAAAAIO8L1AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAADzpJREFUeJzt3X+s3XV9x/Hnix/OOTS264VhWy1z\nda5Ere6GuPHHGG6KJK7oBoGodEhSk2Gmzi1Bt0yzhcRsgNHpSGpAwCCO+Qs0ZBtrjMYfqLcEC6Vj\ndMigtmsvYpTNydb63h/ne8exftqe1n7vOfQ8H8nJOedzv99z3yWFJ+d7zvmeVBWSJO3vuHEPIEma\nTAZCktRkICRJTQZCktRkICRJTQZCktRkICRJTQZCktRkICRJTSeMe4CfxrJly2rVqlXjHkOSnlI2\nb978aFXNHGq7p3QgVq1axdzc3LjHkKSnlCT/Psp2HmKSJDUZCElSk4GQJDUZCElSk4GQJDUZCElS\nk4GQJDUZCElSk4GQJDU9pT9JfTT86p/cOO4RNIE2//XF4x5BGjufQUiSmgyEJKnJQEiSmgyEJKnJ\nQEiSmnoLRJKVST6fZFuSrUne2q2/J8m3k9zdXc4d2uedSbYnuT/Jq/qaTZJ0aH2+zXUv8I6quivJ\nM4HNSe7ofva+qrpyeOMka4ALgdOB5wD/nOQFVbWvxxklSQfQ2zOIqtpVVXd1tx8HtgHLD7LLOuDj\nVfVEVX0L2A6c0dd8kqSDW5TXIJKsAl4KfK1bekuSLUmuS7KkW1sOPDK02w4aQUmyIclckrn5+fke\np5ak6dZ7IJKcBHwSeFtVfR+4Bng+sBbYBVy1sGlj9/qJhaqNVTVbVbMzM4f8zm1J0hHqNRBJTmQQ\nh5uq6lMAVbW7qvZV1Y+AD/PkYaQdwMqh3VcAO/ucT5J0YH2+iynAtcC2qrp6aP3Uoc1eC9zb3b4N\nuDDJzyQ5DVgNfL2v+SRJB9fnu5jOBN4I3JPk7m7tXcBFSdYyOHz0EPBmgKramuQW4D4G74C6zHcw\nSdL49BaIqvoS7dcVbj/IPlcAV/Q1kyRpdH6SWpLUZCAkSU0GQpLUZCAkSU0GQpLUZCAkSU0GQpLU\nZCAkSU0GQpLUZCAkSU0GQpLUZCAkSU0GQpLUZCAkSU0GQpLUZCAkSU0GQpLUZCAkSU0GQpLUZCAk\nSU0GQpLUZCAkSU0GQpLUZCAkSU0GQpLUZCAkSU0GQpLUZCAkSU0GQpLUZCAkSU29BSLJyiSfT7It\nydYkb+3Wlya5I8kD3fWSbj1JPpBke5ItSV7W12ySpEPr8xnEXuAdVfUrwMuBy5KsAS4HNlXVamBT\ndx/g1cDq7rIBuKbH2SRJh9BbIKpqV1Xd1d1+HNgGLAfWATd0m90AnNfdXgfcWAN3As9Ocmpf80mS\nDm5RXoNIsgp4KfA14JSq2gWDiAAnd5stBx4Z2m1HtyZJGoPeA5HkJOCTwNuq6vsH27SxVo3H25Bk\nLsnc/Pz80RpTkrSfXgOR5EQGcbipqj7VLe9eOHTUXe/p1ncAK4d2XwHs3P8xq2pjVc1W1ezMzEx/\nw0vSlOvzXUwBrgW2VdXVQz+6DVjf3V4P3Dq0fnH3bqaXA99bOBQlSVp8J/T42GcCbwTuSXJ3t/Yu\n4L3ALUkuBR4Gzu9+djtwLrAd+AFwSY+zSZIOobdAVNWXaL+uAPCKxvYFXNbXPJKkw+MnqSVJTQZC\nktRkICRJTQZCktRkICRJTQZCktRkICRJTQZCktRkICRJTQZCktRkICRJTQZCktRkICRJTQZCktRk\nICRJTQZCktRkICRJTQZCktRkICRJTQZCktRkICRJTQZCktRkICRJTQZCktRkICRJTQZCktRkICRJ\nTQZCktRkICRJTQZCktRkICRJTb0FIsl1SfYkuXdo7T1Jvp3k7u5y7tDP3plke5L7k7yqr7kkSaMZ\nKRBJNo2ytp/rgXMa6++rqrXd5fbusdYAFwKnd/v8bZLjR5lNktSPgwYiydOTLAWWJVmSZGl3WQU8\n52D7VtUXgcdGnGMd8PGqeqKqvgVsB84YcV9JUg8O9QzizcBm4IXd9cLlVuBDR/g735JkS3cIakm3\nthx4ZGibHd3aT0iyIclckrn5+fkjHEGSdCgHDURVvb+qTgP+uKp+sapO6y4vqaoPHsHvuwZ4PrAW\n2AVc1a2n9esPMNPGqpqtqtmZmZkjGEGSNIoTRtmoqv4mya8Dq4b3qaobD+eXVdXuhdtJPgx8rru7\nA1g5tOkKYOfhPLYk6egaKRBJPsrg//zvBvZ1ywUcViCSnFpVu7q7rwUW3uF0G/CxJFczeG1jNfD1\nw3lsSdLRNVIggFlgTVU1D/u0JLkZOIvBC9w7gHcDZyVZyyAuDzF4jYOq2prkFuA+YC9wWVXtaz2u\nJGlxjBqIe4FfYPC6wUiq6qLG8rUH2f4K4IpRH1+S1K9RA7EMuC/J14EnFhar6nd6mUqSNHajBuI9\nfQ4hSZo8o76L6Qt9DyJJmiyjvovpcZ78XMLTgBOB/6qqZ/U1mCRpvEZ9BvHM4ftJzsNTYUjSMe2I\nzuZaVZ8Bzj7Ks0iSJsioh5heN3T3OAafixj5MxGSpKeeUd/F9Jqh23sZfMht3VGfRpI0MUZ9DeKS\nvgeRJE2WUb8waEWST3ffELc7ySeTrOh7OEnS+Ix6iOkjwMeA87v7b+jWfruPoSTBw3/xonGPoAn0\n3D+/Z9F+16jvYpqpqo9U1d7ucj3glzFI0jFs1EA8muQNSY7vLm8AvtPnYJKk8Ro1EG8CLgD+g8EZ\nXX8P8IVrSTqGjfoaxF8C66vquwBJlgJXMgiHJOkYNOoziBcvxAGgqh4DXtrPSJKkSTBqII5LsmTh\nTvcMYtRnH5Kkp6BR/yN/FfCVJJ9gcIqNC/Db3yTpmDbqJ6lvTDLH4AR9AV5XVff1OpkkaaxGPkzU\nBcEoSNKUOKLTfUuSjn0GQpLUZCAkSU0GQpLUZCAkSU0GQpLUZCAkSU0GQpLUZCAkSU0GQpLU1Fsg\nklyXZE+Se4fWlia5I8kD3fWSbj1JPpBke5ItSV7W11ySpNH0+QzieuCc/dYuBzZV1WpgU3cf4NXA\n6u6yAbimx7kkSSPoLRBV9UXgsf2W1wE3dLdvAM4bWr+xBu4Enp3k1L5mkyQd2mK/BnFKVe0C6K5P\n7taXA48MbbejW5MkjcmkvEidxlo1N0w2JJlLMjc/P9/zWJI0vRY7ELsXDh1113u69R3AyqHtVgA7\nWw9QVRuraraqZmdmZnodVpKm2WIH4jZgfXd7PXDr0PrF3buZXg58b+FQlCRpPEb+RrnDleRm4Cxg\nWZIdwLuB9wK3JLkUeBg4v9v8duBcYDvwA+CSvuaSJI2mt0BU1UUH+NErGtsWcFlfs0iSDt+kvEgt\nSZowBkKS1GQgJElNBkKS1GQgJElNBkKS1GQgJElNBkKS1GQgJElNBkKS1GQgJElNBkKS1GQgJElN\nBkKS1GQgJElNBkKS1GQgJElNBkKS1GQgJElNBkKS1GQgJElNBkKS1GQgJElNBkKS1GQgJElNBkKS\n1GQgJElNBkKS1GQgJElNBkKS1GQgJElNJ4zjlyZ5CHgc2AfsrarZJEuBvwNWAQ8BF1TVd8cxnyRp\nvM8gfrOq1lbVbHf/cmBTVa0GNnX3JUljMkmHmNYBN3S3bwDOG+MskjT1xhWIAv4pyeYkG7q1U6pq\nF0B3fXJrxyQbkswlmZufn1+kcSVp+ozlNQjgzKrameRk4I4k/zLqjlW1EdgIMDs7W30NKEnTbizP\nIKpqZ3e9B/g0cAawO8mpAN31nnHMJkkaWPRAJPm5JM9cuA28ErgXuA1Y3222Hrh1sWeTJD1pHIeY\nTgE+nWTh93+sqv4hyTeAW5JcCjwMnD+G2SRJnUUPRFU9CLyksf4d4BWLPY8kqW2S3uYqSZogBkKS\n1GQgJElNBkKS1GQgJElNBkKS1GQgJElNBkKS1GQgJElNBkKS1GQgJElNBkKS1GQgJElNBkKS1GQg\nJElNBkKS1GQgJElNBkKS1GQgJElNBkKS1GQgJElNBkKS1GQgJElNBkKS1GQgJElNBkKS1GQgJElN\nBkKS1GQgJElNBkKS1DRxgUhyTpL7k2xPcvm455GkaTVRgUhyPPAh4NXAGuCiJGvGO5UkTaeJCgRw\nBrC9qh6sqv8BPg6sG/NMkjSVJi0Qy4FHhu7v6NYkSYvshHEPsJ801urHNkg2ABu6u/+Z5P7ep5oe\ny4BHxz3EJMiV68c9gn6cfzcXvLv1n8nD9rxRNpq0QOwAVg7dXwHsHN6gqjYCGxdzqGmRZK6qZsc9\nh7Q//26Ox6QdYvoGsDrJaUmeBlwI3DbmmSRpKk3UM4iq2pvkLcA/AscD11XV1jGPJUlTaaICAVBV\ntwO3j3uOKeWhO00q/26OQarq0FtJkqbOpL0GIUmaEAZCnt5EEyvJdUn2JLl33LNMIwMx5Ty9iSbc\n9cA54x5iWhkIeXoTTayq+iLw2LjnmFYGQp7eRFKTgdAhT28iaToZCB3y9CaSppOBkKc3kdRkIKZc\nVe0FFk5vsg24xdObaFIkuRn4KvDLSXYkuXTcM00TP0ktSWryGYQkqclASJKaDIQkqclASJKaDIQk\nqclAaOolWZHk1iQPJPm3JO/vPhNysH3etVjzSeNiIDTVkgT4FPCZqloNvAA4CbjiELsaCB3zDISm\n3dnAD6vqIwBVtQ94O/CmJH+Q5IMLGyb5XJKzkrwX+Nkkdye5qfvZxUm2JPlmko92a89Lsqlb35Tk\nud369UmuSfL5JA8m+Y3uew+2Jbl+6Pe9MslXk9yV5O+TnLRo/1QkDIR0OrB5eKGqvg88zAG+s72q\nLgf+u6rWVtXrk5wO/ClwdlW9BHhrt+kHgRur6sXATcAHhh5mCYM4vR34LPC+bpYXJVmbZBnwZ8Bv\nVdXLgDngj47GH1gaVfNfAGmKhPbZaw+03nI28ImqehSgqha+v+DXgNd1tz8K/NXQPp+tqkpyD7C7\nqu4BSLIVWMXgpIlrgC8PjoLxNAannJAWjYHQtNsK/O7wQpJnMTjD7ff48WfZTz/AY4wak+Ftnuiu\nfzR0e+H+CcA+4I6qumiEx5V64SEmTbtNwDOSXAz//xWsVzH4qssHgbVJjkuyksG37y343yQnDj3G\nBUl+vnuMpd36VxicHRfg9cCXDmOuO4Ezk/xS95jPSPKCw/3DST8NA6GpVoOzVb4WOD/JA8C/Aj9k\n8C6lLwPfAu4BrgTuGtp1I7AlyU3d2W+vAL6Q5JvA1d02fwhckmQL8EaefG1ilLnmgd8Hbu72vxN4\n4ZH+OaUj4dlcJUlNPoOQJDUZCElSk4GQJDUZCElSk4GQJDUZCElSk4GQJDUZCElS0/8BUxehEu+I\nUyYAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x191a9247208>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sns.countplot(train.Outcome)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "虽然各类样本不均衡，但数据量也还好，输出1的样本也不是特别少，\n",
    "且交叉验证对分类任务缺省的是采用StratifiedKFold，在每折采样时根据各类样本按比例采样"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "因为diabetesPedigreeFunction的标准差小于1，所以这个特征用MinMaxScaler，其他的用StandardScaler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.preprocessing import  MinMaxScaler\n",
    "\n",
    "mm_X = MinMaxScaler()\n",
    "ss_X = StandardScaler()\n",
    "\n",
    "# 分别对训练和测试数据的特征进行标准化处理\n",
    "pedigree = mm_X.fit_transform(train['DiabetesPedigreeFunction'].values.reshape(-1,1))\n",
    "pedigree = pd.DataFrame(pedigree,columns=['PedigreeFunction'])\n",
    "otherFeature = ss_X.fit_transform(train.iloc[:,[0,1,2,3,4,5]])\n",
    "otherFeature = pd.DataFrame(otherFeature,\n",
    "                            columns=['Pregnancies','Glucose','BloodPressure','SkinThickness','Insulin','BMI'])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "OutCome = train['Outcome'].reset_index(drop=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train = pd.concat([otherFeature,pedigree,OutCome],axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Pregnancies</th>\n",
       "      <th>Glucose</th>\n",
       "      <th>BloodPressure</th>\n",
       "      <th>SkinThickness</th>\n",
       "      <th>Insulin</th>\n",
       "      <th>BMI</th>\n",
       "      <th>PedigreeFunction</th>\n",
       "      <th>Outcome</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.717427</td>\n",
       "      <td>-1.091046</td>\n",
       "      <td>-0.373655</td>\n",
       "      <td>-0.585110</td>\n",
       "      <td>-0.522842</td>\n",
       "      <td>-0.710421</td>\n",
       "      <td>0.035118</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-1.029213</td>\n",
       "      <td>0.466314</td>\n",
       "      <td>-2.456964</td>\n",
       "      <td>0.557421</td>\n",
       "      <td>0.100631</td>\n",
       "      <td>1.426730</td>\n",
       "      <td>0.943469</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.093854</td>\n",
       "      <td>-1.447941</td>\n",
       "      <td>-1.655691</td>\n",
       "      <td>0.271788</td>\n",
       "      <td>-0.573394</td>\n",
       "      <td>-0.297238</td>\n",
       "      <td>0.069807</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.405640</td>\n",
       "      <td>2.413014</td>\n",
       "      <td>-0.053146</td>\n",
       "      <td>1.509530</td>\n",
       "      <td>3.260122</td>\n",
       "      <td>-0.368477</td>\n",
       "      <td>0.031263</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.717427</td>\n",
       "      <td>2.153454</td>\n",
       "      <td>-0.854419</td>\n",
       "      <td>-0.585110</td>\n",
       "      <td>5.812990</td>\n",
       "      <td>-0.425468</td>\n",
       "      <td>0.134047</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.529718</td>\n",
       "      <td>1.407219</td>\n",
       "      <td>0.107109</td>\n",
       "      <td>-0.965953</td>\n",
       "      <td>0.159608</td>\n",
       "      <td>-1.038117</td>\n",
       "      <td>0.214989</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>-1.029213</td>\n",
       "      <td>-0.150141</td>\n",
       "      <td>1.068636</td>\n",
       "      <td>1.699951</td>\n",
       "      <td>0.623000</td>\n",
       "      <td>1.811417</td>\n",
       "      <td>0.199572</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>-0.717427</td>\n",
       "      <td>-0.636816</td>\n",
       "      <td>-3.258237</td>\n",
       "      <td>0.843053</td>\n",
       "      <td>-0.615520</td>\n",
       "      <td>1.455225</td>\n",
       "      <td>0.041970</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>-0.717427</td>\n",
       "      <td>-0.247476</td>\n",
       "      <td>-0.053146</td>\n",
       "      <td>0.081366</td>\n",
       "      <td>-0.505991</td>\n",
       "      <td>0.215678</td>\n",
       "      <td>0.190150</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>-0.093854</td>\n",
       "      <td>0.109419</td>\n",
       "      <td>1.389146</td>\n",
       "      <td>1.128686</td>\n",
       "      <td>0.665127</td>\n",
       "      <td>0.885318</td>\n",
       "      <td>0.265096</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>2.400436</td>\n",
       "      <td>0.660984</td>\n",
       "      <td>1.869909</td>\n",
       "      <td>0.366999</td>\n",
       "      <td>-0.084726</td>\n",
       "      <td>0.500631</td>\n",
       "      <td>0.072377</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>2.088650</td>\n",
       "      <td>0.076974</td>\n",
       "      <td>-0.053146</td>\n",
       "      <td>-0.299477</td>\n",
       "      <td>-0.345911</td>\n",
       "      <td>-0.282991</td>\n",
       "      <td>0.051392</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>-0.717427</td>\n",
       "      <td>-0.831486</td>\n",
       "      <td>-0.373655</td>\n",
       "      <td>-1.346797</td>\n",
       "      <td>-0.135278</td>\n",
       "      <td>-1.408557</td>\n",
       "      <td>0.172163</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>3.024008</td>\n",
       "      <td>0.725874</td>\n",
       "      <td>0.908382</td>\n",
       "      <td>-0.965953</td>\n",
       "      <td>-0.388037</td>\n",
       "      <td>-1.551034</td>\n",
       "      <td>0.068522</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>-0.093854</td>\n",
       "      <td>1.147659</td>\n",
       "      <td>0.427618</td>\n",
       "      <td>0.652632</td>\n",
       "      <td>0.749380</td>\n",
       "      <td>-0.211752</td>\n",
       "      <td>0.328051</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>-0.093854</td>\n",
       "      <td>-1.123491</td>\n",
       "      <td>-1.014673</td>\n",
       "      <td>-1.727640</td>\n",
       "      <td>-0.859854</td>\n",
       "      <td>-1.180594</td>\n",
       "      <td>0.077944</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>0.217932</td>\n",
       "      <td>-0.636816</td>\n",
       "      <td>-0.854419</td>\n",
       "      <td>0.366999</td>\n",
       "      <td>0.302838</td>\n",
       "      <td>-1.294576</td>\n",
       "      <td>0.377302</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>0.217932</td>\n",
       "      <td>-0.377256</td>\n",
       "      <td>0.107109</td>\n",
       "      <td>1.699951</td>\n",
       "      <td>0.429218</td>\n",
       "      <td>0.571870</td>\n",
       "      <td>0.558887</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>-0.093854</td>\n",
       "      <td>1.861449</td>\n",
       "      <td>-0.533909</td>\n",
       "      <td>-0.394688</td>\n",
       "      <td>-0.725049</td>\n",
       "      <td>0.130192</td>\n",
       "      <td>0.079657</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>1.776863</td>\n",
       "      <td>1.569444</td>\n",
       "      <td>3.151946</td>\n",
       "      <td>-0.489899</td>\n",
       "      <td>0.707253</td>\n",
       "      <td>1.754427</td>\n",
       "      <td>0.272377</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>-0.717427</td>\n",
       "      <td>-0.636816</td>\n",
       "      <td>0.748127</td>\n",
       "      <td>-1.727640</td>\n",
       "      <td>-0.623946</td>\n",
       "      <td>-1.949969</td>\n",
       "      <td>0.173876</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>-0.717427</td>\n",
       "      <td>-0.701706</td>\n",
       "      <td>-1.655691</td>\n",
       "      <td>-1.346797</td>\n",
       "      <td>-1.011510</td>\n",
       "      <td>-1.266080</td>\n",
       "      <td>0.188865</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>0.529718</td>\n",
       "      <td>-1.123491</td>\n",
       "      <td>-0.373655</td>\n",
       "      <td>-0.775531</td>\n",
       "      <td>-1.121039</td>\n",
       "      <td>-1.237585</td>\n",
       "      <td>0.110064</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>1.465077</td>\n",
       "      <td>1.731669</td>\n",
       "      <td>1.549400</td>\n",
       "      <td>0.462210</td>\n",
       "      <td>1.212772</td>\n",
       "      <td>0.087449</td>\n",
       "      <td>0.163597</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>1.153291</td>\n",
       "      <td>0.888099</td>\n",
       "      <td>-0.373655</td>\n",
       "      <td>1.223897</td>\n",
       "      <td>1.566635</td>\n",
       "      <td>0.229925</td>\n",
       "      <td>0.271092</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>1.153291</td>\n",
       "      <td>2.088564</td>\n",
       "      <td>-0.213400</td>\n",
       "      <td>0.938264</td>\n",
       "      <td>1.246473</td>\n",
       "      <td>0.657356</td>\n",
       "      <td>0.072377</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>-1.029213</td>\n",
       "      <td>-0.734151</td>\n",
       "      <td>1.389146</td>\n",
       "      <td>2.937693</td>\n",
       "      <td>-0.388037</td>\n",
       "      <td>1.953894</td>\n",
       "      <td>0.375589</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>-1.029213</td>\n",
       "      <td>-0.571926</td>\n",
       "      <td>-0.533909</td>\n",
       "      <td>1.128686</td>\n",
       "      <td>-0.118427</td>\n",
       "      <td>1.198767</td>\n",
       "      <td>0.037687</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>-0.405640</td>\n",
       "      <td>0.596094</td>\n",
       "      <td>-1.014673</td>\n",
       "      <td>0.462210</td>\n",
       "      <td>-0.236382</td>\n",
       "      <td>-1.095108</td>\n",
       "      <td>0.262955</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>-0.717427</td>\n",
       "      <td>-0.896376</td>\n",
       "      <td>-0.373655</td>\n",
       "      <td>-1.537218</td>\n",
       "      <td>-0.994659</td>\n",
       "      <td>-1.921473</td>\n",
       "      <td>0.106638</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>362</th>\n",
       "      <td>-0.405640</td>\n",
       "      <td>0.141864</td>\n",
       "      <td>-1.976201</td>\n",
       "      <td>-0.775531</td>\n",
       "      <td>1.507657</td>\n",
       "      <td>0.187182</td>\n",
       "      <td>0.038972</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>363</th>\n",
       "      <td>-0.405640</td>\n",
       "      <td>-0.961266</td>\n",
       "      <td>-0.533909</td>\n",
       "      <td>0.271788</td>\n",
       "      <td>0.033228</td>\n",
       "      <td>0.700099</td>\n",
       "      <td>0.252248</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>364</th>\n",
       "      <td>-0.093854</td>\n",
       "      <td>1.147659</td>\n",
       "      <td>-0.533909</td>\n",
       "      <td>-1.537218</td>\n",
       "      <td>1.945774</td>\n",
       "      <td>-0.268743</td>\n",
       "      <td>0.089936</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>365</th>\n",
       "      <td>0.529718</td>\n",
       "      <td>0.109419</td>\n",
       "      <td>0.587873</td>\n",
       "      <td>-0.204266</td>\n",
       "      <td>-1.129464</td>\n",
       "      <td>-0.496706</td>\n",
       "      <td>0.151606</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>366</th>\n",
       "      <td>-1.029213</td>\n",
       "      <td>0.368979</td>\n",
       "      <td>-1.014673</td>\n",
       "      <td>-0.870742</td>\n",
       "      <td>1.136944</td>\n",
       "      <td>-0.952631</td>\n",
       "      <td>0.114347</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>367</th>\n",
       "      <td>1.153291</td>\n",
       "      <td>2.088564</td>\n",
       "      <td>-1.655691</td>\n",
       "      <td>0.366999</td>\n",
       "      <td>1.987900</td>\n",
       "      <td>0.115944</td>\n",
       "      <td>0.317345</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>368</th>\n",
       "      <td>-0.093854</td>\n",
       "      <td>1.634334</td>\n",
       "      <td>0.587873</td>\n",
       "      <td>0.938264</td>\n",
       "      <td>0.243861</td>\n",
       "      <td>0.101696</td>\n",
       "      <td>0.379015</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>369</th>\n",
       "      <td>-0.717427</td>\n",
       "      <td>-0.474591</td>\n",
       "      <td>-0.854419</td>\n",
       "      <td>1.604740</td>\n",
       "      <td>0.184884</td>\n",
       "      <td>0.343907</td>\n",
       "      <td>0.141328</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>370</th>\n",
       "      <td>-0.717427</td>\n",
       "      <td>-0.279921</td>\n",
       "      <td>-0.373655</td>\n",
       "      <td>0.652632</td>\n",
       "      <td>0.370241</td>\n",
       "      <td>0.714346</td>\n",
       "      <td>0.087366</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>371</th>\n",
       "      <td>-0.717427</td>\n",
       "      <td>0.855654</td>\n",
       "      <td>-0.213400</td>\n",
       "      <td>-0.013844</td>\n",
       "      <td>-0.244807</td>\n",
       "      <td>-0.539449</td>\n",
       "      <td>0.113062</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>372</th>\n",
       "      <td>0.529718</td>\n",
       "      <td>-0.182586</td>\n",
       "      <td>1.228891</td>\n",
       "      <td>0.081366</td>\n",
       "      <td>-0.430164</td>\n",
       "      <td>0.856823</td>\n",
       "      <td>0.071092</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>373</th>\n",
       "      <td>-0.717427</td>\n",
       "      <td>-0.215031</td>\n",
       "      <td>0.587873</td>\n",
       "      <td>-0.013844</td>\n",
       "      <td>0.201735</td>\n",
       "      <td>0.429393</td>\n",
       "      <td>0.176017</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>374</th>\n",
       "      <td>-0.093854</td>\n",
       "      <td>0.239199</td>\n",
       "      <td>0.587873</td>\n",
       "      <td>-0.585110</td>\n",
       "      <td>-0.649222</td>\n",
       "      <td>-0.667678</td>\n",
       "      <td>0.101927</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>375</th>\n",
       "      <td>-0.405640</td>\n",
       "      <td>1.666779</td>\n",
       "      <td>1.389146</td>\n",
       "      <td>0.747843</td>\n",
       "      <td>-0.303784</td>\n",
       "      <td>1.626197</td>\n",
       "      <td>0.240257</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>376</th>\n",
       "      <td>-0.405640</td>\n",
       "      <td>-0.539481</td>\n",
       "      <td>-1.174928</td>\n",
       "      <td>-0.204266</td>\n",
       "      <td>0.075355</td>\n",
       "      <td>-0.582192</td>\n",
       "      <td>0.146039</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>377</th>\n",
       "      <td>-1.029213</td>\n",
       "      <td>0.109419</td>\n",
       "      <td>1.228891</td>\n",
       "      <td>-0.204266</td>\n",
       "      <td>-0.303784</td>\n",
       "      <td>-0.810155</td>\n",
       "      <td>0.184154</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>378</th>\n",
       "      <td>-0.405640</td>\n",
       "      <td>-0.766596</td>\n",
       "      <td>-0.854419</td>\n",
       "      <td>-1.156375</td>\n",
       "      <td>0.033228</td>\n",
       "      <td>0.500631</td>\n",
       "      <td>0.157602</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>379</th>\n",
       "      <td>2.400436</td>\n",
       "      <td>-0.085251</td>\n",
       "      <td>0.748127</td>\n",
       "      <td>0.747843</td>\n",
       "      <td>-0.051025</td>\n",
       "      <td>1.312749</td>\n",
       "      <td>0.299786</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>380</th>\n",
       "      <td>-0.093854</td>\n",
       "      <td>-0.669261</td>\n",
       "      <td>-2.136455</td>\n",
       "      <td>-0.870742</td>\n",
       "      <td>-0.522842</td>\n",
       "      <td>-0.325734</td>\n",
       "      <td>0.134904</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>381</th>\n",
       "      <td>-0.717427</td>\n",
       "      <td>-0.442146</td>\n",
       "      <td>-1.014673</td>\n",
       "      <td>-1.061164</td>\n",
       "      <td>-0.337485</td>\n",
       "      <td>-0.653430</td>\n",
       "      <td>0.057388</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>382</th>\n",
       "      <td>3.024008</td>\n",
       "      <td>0.985434</td>\n",
       "      <td>1.389146</td>\n",
       "      <td>0.747843</td>\n",
       "      <td>-0.135278</td>\n",
       "      <td>1.070538</td>\n",
       "      <td>0.466381</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>383</th>\n",
       "      <td>2.712222</td>\n",
       "      <td>-0.734151</td>\n",
       "      <td>1.068636</td>\n",
       "      <td>0.366999</td>\n",
       "      <td>-0.430164</td>\n",
       "      <td>-0.439715</td>\n",
       "      <td>0.172591</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>384</th>\n",
       "      <td>-0.717427</td>\n",
       "      <td>-1.350606</td>\n",
       "      <td>0.267364</td>\n",
       "      <td>1.128686</td>\n",
       "      <td>-0.834578</td>\n",
       "      <td>1.882656</td>\n",
       "      <td>0.432976</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>385</th>\n",
       "      <td>-0.093854</td>\n",
       "      <td>2.088564</td>\n",
       "      <td>-0.053146</td>\n",
       "      <td>-0.680321</td>\n",
       "      <td>0.370241</td>\n",
       "      <td>0.472136</td>\n",
       "      <td>0.138330</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>386</th>\n",
       "      <td>-0.717427</td>\n",
       "      <td>-0.052806</td>\n",
       "      <td>0.587873</td>\n",
       "      <td>0.938264</td>\n",
       "      <td>-0.691348</td>\n",
       "      <td>0.842575</td>\n",
       "      <td>0.075375</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>387</th>\n",
       "      <td>-1.029213</td>\n",
       "      <td>1.893894</td>\n",
       "      <td>1.389146</td>\n",
       "      <td>1.414319</td>\n",
       "      <td>2.982087</td>\n",
       "      <td>1.455225</td>\n",
       "      <td>0.058672</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>388</th>\n",
       "      <td>-0.717427</td>\n",
       "      <td>0.174309</td>\n",
       "      <td>1.389146</td>\n",
       "      <td>0.938264</td>\n",
       "      <td>-0.388037</td>\n",
       "      <td>0.486384</td>\n",
       "      <td>0.416274</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>389</th>\n",
       "      <td>-0.405640</td>\n",
       "      <td>-1.123491</td>\n",
       "      <td>-1.014673</td>\n",
       "      <td>-0.299477</td>\n",
       "      <td>-1.180016</td>\n",
       "      <td>-0.667678</td>\n",
       "      <td>0.291649</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>390</th>\n",
       "      <td>2.088650</td>\n",
       "      <td>-0.701706</td>\n",
       "      <td>0.427618</td>\n",
       "      <td>1.795162</td>\n",
       "      <td>0.201735</td>\n",
       "      <td>-0.026533</td>\n",
       "      <td>0.036831</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>391</th>\n",
       "      <td>0.529718</td>\n",
       "      <td>-0.052806</td>\n",
       "      <td>0.107109</td>\n",
       "      <td>-0.585110</td>\n",
       "      <td>-0.371186</td>\n",
       "      <td>-0.981127</td>\n",
       "      <td>0.068522</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>392 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     Pregnancies   Glucose  BloodPressure  SkinThickness   Insulin       BMI  \\\n",
       "0      -0.717427 -1.091046      -0.373655      -0.585110 -0.522842 -0.710421   \n",
       "1      -1.029213  0.466314      -2.456964       0.557421  0.100631  1.426730   \n",
       "2      -0.093854 -1.447941      -1.655691       0.271788 -0.573394 -0.297238   \n",
       "3      -0.405640  2.413014      -0.053146       1.509530  3.260122 -0.368477   \n",
       "4      -0.717427  2.153454      -0.854419      -0.585110  5.812990 -0.425468   \n",
       "5       0.529718  1.407219       0.107109      -0.965953  0.159608 -1.038117   \n",
       "6      -1.029213 -0.150141       1.068636       1.699951  0.623000  1.811417   \n",
       "7      -0.717427 -0.636816      -3.258237       0.843053 -0.615520  1.455225   \n",
       "8      -0.717427 -0.247476      -0.053146       0.081366 -0.505991  0.215678   \n",
       "9      -0.093854  0.109419       1.389146       1.128686  0.665127  0.885318   \n",
       "10      2.400436  0.660984       1.869909       0.366999 -0.084726  0.500631   \n",
       "11      2.088650  0.076974      -0.053146      -0.299477 -0.345911 -0.282991   \n",
       "12     -0.717427 -0.831486      -0.373655      -1.346797 -0.135278 -1.408557   \n",
       "13      3.024008  0.725874       0.908382      -0.965953 -0.388037 -1.551034   \n",
       "14     -0.093854  1.147659       0.427618       0.652632  0.749380 -0.211752   \n",
       "15     -0.093854 -1.123491      -1.014673      -1.727640 -0.859854 -1.180594   \n",
       "16      0.217932 -0.636816      -0.854419       0.366999  0.302838 -1.294576   \n",
       "17      0.217932 -0.377256       0.107109       1.699951  0.429218  0.571870   \n",
       "18     -0.093854  1.861449      -0.533909      -0.394688 -0.725049  0.130192   \n",
       "19      1.776863  1.569444       3.151946      -0.489899  0.707253  1.754427   \n",
       "20     -0.717427 -0.636816       0.748127      -1.727640 -0.623946 -1.949969   \n",
       "21     -0.717427 -0.701706      -1.655691      -1.346797 -1.011510 -1.266080   \n",
       "22      0.529718 -1.123491      -0.373655      -0.775531 -1.121039 -1.237585   \n",
       "23      1.465077  1.731669       1.549400       0.462210  1.212772  0.087449   \n",
       "24      1.153291  0.888099      -0.373655       1.223897  1.566635  0.229925   \n",
       "25      1.153291  2.088564      -0.213400       0.938264  1.246473  0.657356   \n",
       "26     -1.029213 -0.734151       1.389146       2.937693 -0.388037  1.953894   \n",
       "27     -1.029213 -0.571926      -0.533909       1.128686 -0.118427  1.198767   \n",
       "28     -0.405640  0.596094      -1.014673       0.462210 -0.236382 -1.095108   \n",
       "29     -0.717427 -0.896376      -0.373655      -1.537218 -0.994659 -1.921473   \n",
       "..           ...       ...            ...            ...       ...       ...   \n",
       "362    -0.405640  0.141864      -1.976201      -0.775531  1.507657  0.187182   \n",
       "363    -0.405640 -0.961266      -0.533909       0.271788  0.033228  0.700099   \n",
       "364    -0.093854  1.147659      -0.533909      -1.537218  1.945774 -0.268743   \n",
       "365     0.529718  0.109419       0.587873      -0.204266 -1.129464 -0.496706   \n",
       "366    -1.029213  0.368979      -1.014673      -0.870742  1.136944 -0.952631   \n",
       "367     1.153291  2.088564      -1.655691       0.366999  1.987900  0.115944   \n",
       "368    -0.093854  1.634334       0.587873       0.938264  0.243861  0.101696   \n",
       "369    -0.717427 -0.474591      -0.854419       1.604740  0.184884  0.343907   \n",
       "370    -0.717427 -0.279921      -0.373655       0.652632  0.370241  0.714346   \n",
       "371    -0.717427  0.855654      -0.213400      -0.013844 -0.244807 -0.539449   \n",
       "372     0.529718 -0.182586       1.228891       0.081366 -0.430164  0.856823   \n",
       "373    -0.717427 -0.215031       0.587873      -0.013844  0.201735  0.429393   \n",
       "374    -0.093854  0.239199       0.587873      -0.585110 -0.649222 -0.667678   \n",
       "375    -0.405640  1.666779       1.389146       0.747843 -0.303784  1.626197   \n",
       "376    -0.405640 -0.539481      -1.174928      -0.204266  0.075355 -0.582192   \n",
       "377    -1.029213  0.109419       1.228891      -0.204266 -0.303784 -0.810155   \n",
       "378    -0.405640 -0.766596      -0.854419      -1.156375  0.033228  0.500631   \n",
       "379     2.400436 -0.085251       0.748127       0.747843 -0.051025  1.312749   \n",
       "380    -0.093854 -0.669261      -2.136455      -0.870742 -0.522842 -0.325734   \n",
       "381    -0.717427 -0.442146      -1.014673      -1.061164 -0.337485 -0.653430   \n",
       "382     3.024008  0.985434       1.389146       0.747843 -0.135278  1.070538   \n",
       "383     2.712222 -0.734151       1.068636       0.366999 -0.430164 -0.439715   \n",
       "384    -0.717427 -1.350606       0.267364       1.128686 -0.834578  1.882656   \n",
       "385    -0.093854  2.088564      -0.053146      -0.680321  0.370241  0.472136   \n",
       "386    -0.717427 -0.052806       0.587873       0.938264 -0.691348  0.842575   \n",
       "387    -1.029213  1.893894       1.389146       1.414319  2.982087  1.455225   \n",
       "388    -0.717427  0.174309       1.389146       0.938264 -0.388037  0.486384   \n",
       "389    -0.405640 -1.123491      -1.014673      -0.299477 -1.180016 -0.667678   \n",
       "390     2.088650 -0.701706       0.427618       1.795162  0.201735 -0.026533   \n",
       "391     0.529718 -0.052806       0.107109      -0.585110 -0.371186 -0.981127   \n",
       "\n",
       "     PedigreeFunction  Outcome  \n",
       "0            0.035118        0  \n",
       "1            0.943469        1  \n",
       "2            0.069807        1  \n",
       "3            0.031263        1  \n",
       "4            0.134047        1  \n",
       "5            0.214989        1  \n",
       "6            0.199572        1  \n",
       "7            0.041970        0  \n",
       "8            0.190150        1  \n",
       "9            0.265096        0  \n",
       "10           0.072377        1  \n",
       "11           0.051392        1  \n",
       "12           0.172163        0  \n",
       "13           0.068522        0  \n",
       "14           0.328051        1  \n",
       "15           0.077944        0  \n",
       "16           0.377302        0  \n",
       "17           0.558887        1  \n",
       "18           0.079657        0  \n",
       "19           0.272377        1  \n",
       "20           0.173876        0  \n",
       "21           0.188865        0  \n",
       "22           0.110064        0  \n",
       "23           0.163597        1  \n",
       "24           0.271092        0  \n",
       "25           0.072377        1  \n",
       "26           0.375589        0  \n",
       "27           0.037687        0  \n",
       "28           0.262955        0  \n",
       "29           0.106638        0  \n",
       "..                ...      ...  \n",
       "362          0.038972        0  \n",
       "363          0.252248        1  \n",
       "364          0.089936        0  \n",
       "365          0.151606        0  \n",
       "366          0.114347        0  \n",
       "367          0.317345        1  \n",
       "368          0.379015        1  \n",
       "369          0.141328        0  \n",
       "370          0.087366        0  \n",
       "371          0.113062        1  \n",
       "372          0.071092        0  \n",
       "373          0.176017        0  \n",
       "374          0.101927        1  \n",
       "375          0.240257        1  \n",
       "376          0.146039        0  \n",
       "377          0.184154        0  \n",
       "378          0.157602        0  \n",
       "379          0.299786        1  \n",
       "380          0.134904        0  \n",
       "381          0.057388        0  \n",
       "382          0.466381        0  \n",
       "383          0.172591        0  \n",
       "384          0.432976        0  \n",
       "385          0.138330        1  \n",
       "386          0.075375        0  \n",
       "387          0.058672        1  \n",
       "388          0.416274        1  \n",
       "389          0.291649        0  \n",
       "390          0.036831        0  \n",
       "391          0.068522        0  \n",
       "\n",
       "[392 rows x 8 columns]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "y_train = train['Outcome']\n",
    "X_train = train.drop(['Outcome'], axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 模型训练"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### default Logistic Regression"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "lr= LogisticRegression()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "logloss of each fold is:  [ 0.79746835  0.6835443   0.76923077  0.82051282  0.83333333]\n",
      "cv logloss is: 0.780817916261\n"
     ]
    }
   ],
   "source": [
    "# 交叉验证用于评估模型性能和进行参数调优（模型选择）\n",
    "#分类任务中交叉验证缺省是采用StratifiedKFold\n",
    "from sklearn.cross_validation import cross_val_score\n",
    "accuracy = cross_val_score(lr, X_train, y_train, cv=5, scoring='accuracy')\n",
    "print('logloss of each fold is: ',accuracy)\n",
    "print('cv logloss is:', accuracy.mean())\n",
    "#precision_recall_curve"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 正则化的 Logistic Regression及参数调优"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "\n",
    "#需要调优的参数\n",
    "# 请尝试将L1正则和L2正则分开，并配合合适的优化求解算法（slover）\n",
    "#tuned_parameters = {'penalty':['l1','l2'],\n",
    "#                   'C': [0.001, 0.01, 0.1, 1, 10, 100, 1000]\n",
    "#                   }\n",
    "penaltys = ['l1','l2']\n",
    "Cs = [0.001, 0.01, 0.1,6,7, 7.5, 10, 12, 1000]\n",
    "tuned_parameters = dict(penalty = penaltys, C = Cs)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=5, error_score='raise',\n",
       "       estimator=LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,\n",
       "          intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,\n",
       "          penalty='l2', random_state=None, solver='liblinear', tol=0.0001,\n",
       "          verbose=0, warm_start=False),\n",
       "       fit_params=None, iid=True, n_jobs=1,\n",
       "       param_grid={'penalty': ['l1', 'l2'], 'C': [0.001, 0.01, 0.1, 6, 7, 7.5, 10, 12, 1000]},\n",
       "       pre_dispatch='2*n_jobs', refit=True, return_train_score='warn',\n",
       "       scoring='accuracy', verbose=0)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lr_penalty= LogisticRegression()\n",
    "grid= GridSearchCV(lr_penalty, tuned_parameters,cv=5, scoring='accuracy')\n",
    "grid.fit(X_train,y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.790816326531\n",
      "{'C': 6, 'penalty': 'l1'}\n"
     ]
    }
   ],
   "source": [
    "print(grid.best_score_)\n",
    "print(grid.best_params_)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "用logistic回归时，最佳参数是选用 L1正则，且C=6,均值0.790816326531"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 线性SVM正则参数调优"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.当duals = False时"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "penaltys = ['l2','l1']\n",
    "Cs = [0.001, 0.01, 0.1, 1,6,7,8,9, 10, 100,110, 1000]\n",
    "duals=[False]\n",
    "tuned_parameters = dict(penalty = penaltys, C = Cs, dual=duals)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=5, error_score='raise',\n",
       "       estimator=LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True,\n",
       "     intercept_scaling=1, loss='squared_hinge', max_iter=1000,\n",
       "     multi_class='ovr', penalty='l2', random_state=None, tol=0.0001,\n",
       "     verbose=0),\n",
       "       fit_params=None, iid=True, n_jobs=1,\n",
       "       param_grid={'penalty': ['l2', 'l1'], 'C': [0.001, 0.01, 0.1, 1, 6, 7, 8, 9, 10, 100, 110, 1000], 'dual': [False]},\n",
       "       pre_dispatch='2*n_jobs', refit=True, return_train_score='warn',\n",
       "       scoring='accuracy', verbose=0)"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.svm import LinearSVC\n",
    "defaultSVC = LinearSVC()\n",
    "grid= GridSearchCV(defaultSVC, tuned_parameters, cv=5, scoring='accuracy')\n",
    "grid.fit(X_train,y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.78826530612244894"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grid.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'C': 100, 'dual': False, 'penalty': 'l2'}"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grid.best_params_"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.当duals = True时"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "penaltys = ['l2']\n",
    "Cs = [0.001, 0.01, 0.1, 1,6,7,8,9, 10, 100,110, 1000]\n",
    "tuned_parameters = dict(penalty = penaltys, C = Cs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=5, error_score='raise',\n",
       "       estimator=LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True,\n",
       "     intercept_scaling=1, loss='squared_hinge', max_iter=1000,\n",
       "     multi_class='ovr', penalty='l2', random_state=None, tol=0.0001,\n",
       "     verbose=0),\n",
       "       fit_params=None, iid=True, n_jobs=1,\n",
       "       param_grid={'penalty': ['l2'], 'C': [0.001, 0.01, 0.1, 1, 6, 7, 8, 9, 10, 100, 110, 1000]},\n",
       "       pre_dispatch='2*n_jobs', refit=True, return_train_score='warn',\n",
       "       scoring='accuracy', verbose=0)"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.svm import LinearSVC\n",
    "defaultSVC = LinearSVC()\n",
    "grid= GridSearchCV(defaultSVC, tuned_parameters, cv=5, scoring='accuracy')\n",
    "grid.fit(X_train,y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.79081632653061229"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grid.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'C': 9, 'penalty': 'l2'}"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grid.best_params_"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "线性SVM选择L2正则，C=9时，准确率最高：0.79081632653061229 和 Logistic回归准确率一样，\n",
    "\n",
    "想不明白为什么？\n",
    "\n",
    "另外想知道为什么dual 选择 true 时比选择 false 效果好?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### RBF核SVM正则参数调优"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.svm import SVC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "rbfSVC =  SVC(kernel='rbf')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "C_s = np.logspace(-2, 2, 5)\n",
    "gamma_s = np.logspace(-4, 1, 6)  \n",
    "tuned_parameters = dict(gamma = gamma_s, C = C_s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=5, error_score='raise',\n",
       "       estimator=SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,\n",
       "  decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf',\n",
       "  max_iter=-1, probability=False, random_state=None, shrinking=True,\n",
       "  tol=0.001, verbose=False),\n",
       "       fit_params=None, iid=True, n_jobs=1,\n",
       "       param_grid={'gamma': array([  1.00000e-04,   1.00000e-03,   1.00000e-02,   1.00000e-01,\n",
       "         1.00000e+00,   1.00000e+01]), 'C': array([  1.00000e-02,   1.00000e-01,   1.00000e+00,   1.00000e+01,\n",
       "         1.00000e+02])},\n",
       "       pre_dispatch='2*n_jobs', refit=True, return_train_score='warn',\n",
       "       scoring='accuracy', verbose=0)"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grid= GridSearchCV(rbfSVC, tuned_parameters, cv=5, scoring='accuracy')\n",
    "grid.fit(X_train,y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.78316326530612246"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grid.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'C': 1.0, 'gamma': 0.01}"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grid.best_params_"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "RBF核的SVM最佳参数：C=1，gamma = 0.01, 最优准确率 0.783163"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
